﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.fmtx.Model;
using com.fmtx.IDal;
using fmtx.Framework.Expend;

namespace com.fmtx.Dal
{
    public class PictureServer:BaseServer<Picture>,IPicture
    {
        public new bool Add(Picture bean)
        {
            UserInfoServer ui = new UserInfoServer();
            var u = ui.Get(bean.Uid);
            u.ArticleCount++;
            ui.Update(u);
            return base.Add(bean);
        }

        public new bool Delete(int id)
        {
            var a = db.Load(id);
            UserInfoServer ui = new UserInfoServer();
            var u = ui.Get(a.Uid);
            u.ArticleCount--;
            ui.Update(u);
            return base.Delete(id);
        }

        public IList<Picture> Query(int index, int size)
        {
            var list = db.AllList(w =>w.Statu == Approved.IsPass);
            list = list.OrderByDescending(o => o.Id).Skip(index * size).Take(size);
            return list.ToList();
        }

        public IList<Picture> QureyHost(int index, int size, string title, string keyword)
        {
            var list = db.AllList(w =>w.Statu == Approved.IsPass);
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(w => w.Title.Contains(title));
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(w => (w.KeyWord != null && w.KeyWord.Contains(keyword)) || (w.Address != null && w.Address.Contains(keyword)) || (w.Title != null && w.Title.Contains(keyword)));
            }
            
            list = list.OrderByDescending(o => o.ShowCount);
            return list.Skip(index * size).Take(size).ToList();
        }

        public IList<Picture> Query(int size, int uid, string address)
        {
            throw new NotImplementedException();
        }

        public IList<Picture> QueryHost(int size, int uid, string address)
        {
            throw new NotImplementedException();
        }


        public IList<Picture> QueryByUser(int uid, int typeid)
        {
            return db.AllList(w => w.Uid == uid && w.TypeId == typeid).OrderByDescending(o=>o.Id).ToList();
        }


        public IList<Picture> QueryAbout(int size, string address)
        {
            var list = db.AllList();
            if (!string.IsNullOrEmpty(address))
            {
                list = list.Where(w => w.AreaKeys != null && w.AreaKeys.Any(a => a.Name == address));
            }
            return list.DistinctBy(d => d.TypeId).OrderByDescending(o => o.Id).Take(size).ToList();
        }


        public IList<Picture> QureyPraise(int index, int size, string title, string keyword)
        {
            var list = db.AllList(w => w.Statu == Approved.IsPass);
            if (!string.IsNullOrEmpty(title))
            {
                list = list.Where(w => w.Title.Contains(title));
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(w => (w.KeyWord != null && w.KeyWord.Contains(keyword)) || (w.Address != null && w.Address.Contains(keyword)) || (w.Title != null && w.Title.Contains(keyword)));
            }
            list = list.OrderByDescending(o => o.PraiseCount);
            return list.Skip(index * size).Take(size).ToList();
        }


        public IList<Picture> QueryByKeyword(int size, string keyword)
        {
            var list = db.AllList(w=>w.Statu==Approved.IsPass);
            if (!string.IsNullOrEmpty(keyword))
            {
                list = list.Where(w => (w.KeyWord != null && w.KeyWord.Contains(keyword)) || (w.Address != null && w.Address.Contains(keyword)) || (w.Title != null && w.Title.Contains(keyword)));
            }
            var relist = list.Take(size).ToList();
            int count = relist.Count;
            if (count < size)
            {
                List<int> idlist = relist.Select(s => s.Id).ToList();
                var otherlist = db.AllList(w => w.Statu == Approved.IsPass && !idlist.Contains(w.Id)).Take(size-count).ToList();
                relist.AddRange(otherlist);
            }
            return relist;
        }
    }
}
